//长距离测试
//sml_move(400, 1800, 800, 230, 1000);
//短距离测试
//sml_move(400, 1000, 400, 300, 1000);
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    }
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    }

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    //log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    log(xxy);
    gesture.apply(null, xxy);
}


//sml_move(400, 1800, 800, 230, 2000);

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
            break;
        default:
            return 0;
            break;
    }
}

function up() {
    let rNum = randomNum(100, 400);
    log("up");
    log("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down() {
    let rNum = randomNum(100, 400);
    let yNum = randomNum(950, 1000);
    log("down");
    log("x:" + rNum);
    log("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function up1(rNum) {
    log("up");
    log("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down1(rNum, yNum) {
    log("down");
    log("x:" + rNum);
    log("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function root_up() {
    let x1 = randomNum(66,180);
    let x2 = randomNum(66,180);
    let y1 = randomNum(900,1140);
    let y2 = randomNum(146,200);
    log("root_up:x1:,y1:,x2:,y2:",x1,y1,x2,y2);
    ra.swipe(x1, y1, x2, y2);
}

function root_down() {
    let x1 = randomNum(66,180);
    let x2 = randomNum(66,180);
    let y1 = randomNum(146,200);
    let y2 = randomNum(900,1140);
    log("root_up:x1:,y1:,x2:,y2:",x1,y1,x2,y2);
    ra.swipe(x1, y1, x2, y2);
}

function closeApp(packagename) {
    var sh = new Shell(true);
    //注册一个对象运用shell命令，true(真) 以root权限运行代码，默认为falae假
    // console.log("am force-stop"+" "+packagename)
    sh.exec("am force-stop" + " " + packagename); //执行代码运行中属于异步运行
    //上面值com.android.browser是浏览器的包名。自行修改成想停止软件的包名
    sleep(1000);
    //给点延迟让前面的运行命令，一会软件就会关闭
    sh.exit;
    //退出Shell命令，正在执行的命令会被强制退出。所以上面加延迟
    toast("关闭" + packagename);
}

function closeApk(packageName) {
    var name = getPackageName(packageName);
    if (!name) {
        if (getAppName(packageName)) {
            name = packageName;
        } else {
            return false;
        }
    }
    app.openAppSetting(name);
    text(app.getAppName(name)).waitFor();
    let is_sure = textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne();
    if (is_sure.enabled()) {
        textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne().click();
        textMatches(/(.*确.*|.*定.*)/).findOne().click();
        log(app.getAppName(name) + "应用已被关闭");
        sleep(1000);
        back();
    } else {
        log(app.getAppName(name) + "应用不能被正常关闭或不在后台运行");
        back();
    }
}




var p1 = 1000;
var p2 = 2000;
var p3 = 3000;
var p4 = 4000;
var p5 = 5000;

var config = storages.create("auto_reader_config");
var app_list = config.get('app_list');
log(app_list);
// 阅读模式：1 运行时长；2 阅读篇数
var exec_pattern = config.get('exec_pattern');
log(exec_pattern);
// 每个运行时长(分钟)
var run_time_sp = config.get('run_time_sp');
log(run_time_sp);
// 阅读篇数(篇)
var read_count_sp = config.get('read_count_sp');
log(read_count_sp);
// 运行模式:1 时段运行; 2 单次运行
var exec_count = config.get('exec_count');
log(exec_count);
// 运行时段(时)：开始
var begin_time_sp = config.get('begin_time_sp');
log(begin_time_sp);
// 运行时段(时)：结束
var end_time_sp = config.get('end_time_sp');
log(end_time_sp);

auto();
auto.waitFor();

console.setPosition(215, 400);
console.show();

var ra = new RootAutomator();

let msg = "m.5qf8m1.cn";

let packageName = app.getPackageName("微信");
log(packageName);

function startApp(){
    closeApk(packageName);

    app.launchPackage(packageName);
    sleep(p5);

    textContains("微信").waitFor();
    sleep(p1);

    while(true){
        sleep(1000);
        let ca = currentActivity();
        log(ca);
        if(ca == 'com.tencent.mm.ui.LauncherUI'){
            break;
        }
    }
    
}


root_up();

/*
startApp();

function sendMsg(){
    let lds = click("磊帝森");
    log("群组:",lds);
    sleep(p2);
    
    setText(msg);
    sleep(p2);
    
    let fs = click("发送");
    log("发送:",fs);
    sleep(p2);
    
    // let last_msg = click(420, 1025);

    //让"手指1"点击位置(100, 100)
    ra.tap(420, 1025, 1);
    log("root点击:");
    sleep(p5);

    closeTips();
}

sendMsg();

begin();

function begin() {
    var count = 0;
    while (true) {
        log('count:', count);
        checkActivity();
        closeTips();
        let ck = checkRead();
        if(ck){
            read();
        }else{
            // 除数是4的时候，取余数为0的时候刷新
            if(0 == count%4){
                log("除数是4的时候，取余数为0的时候刷新");
                refresh();
            }
        }
        count++;
    };
};

function closeTips(){
    let tips = id("closeTips").findOne(p5);   
    if (tips) {
        log("tips,close");
        tips.click();
    };    
};

function refresh(){
    click(670,94);
    sleep(2000);
    let r = textContains("刷新").findOne(5000);
    log("刷新控件:");
    if(null != r){
        log("有");
        let p = r.parent();
        log("刷新parent控件:");
        if(null !=p){
            log("有");
            p.click();
        }
    }
    sleep(2000);
}

function checkRead(){
    let have = textContains("下一批文章").findOne(5000);
    let flag = true;
    if(null !=have){
        flag=false;
        // refresh();
        sleep(2000);
    }
    sleep(1000);
    log("checkRead:",flag);
    return flag;
};

function checkActivity(){
    sleep(1000);
    let ca = currentActivity();
    // log(ca);
    if(ca != 'com.tencent.mm.plugin.webview.ui.tools.WebviewMpUI'){
        log("不在WEBview Activity");
        //先判断是否在微信中
        if(currentActivity().search('com.tencent.mm') == -1){
            startApp();
            sendMsg();
        }
    }
    sleep(1000);
};

function read() {
    let scroll_count = randomNum(3, 5);
    sleep(p5)
    for (let i = 0; i <= scroll_count; i++) {
        log('----------');
        log('total_count:', scroll_count);
        log('current_count:', i);
        let rNum = randomNum(80, 180);
        log('rNum:', rNum);
        sleep(p2);
        up1(rNum);
        let time = randomNum(6, 8);
        log('time:', time);
        let pt = time * 1000;
        sleep(pt);
    };
    let zan = id("js_bottom_zan_btn").findOne(3000);
    if(null != zan){
        log("zan:点赞");
        zan.click();
    }
    sleep(3000);
    back();
    sleep(2000);
}
*/